$(function(){
    //活动购买价格
    (function(){
        var numInput=$('.number-box .num');
        var moneyRadio=$('.money-radio:checked');
        var dateRadio=$('.date-radio:checked');
        var payNum=numInput.val();
        var payMoney=moneyRadio.val()?moneyRadio.val():0;
        var price=Number(payNum).mul(moneyRadio.val(payMoney));
        var tip=moneyRadio.attr('data-value');
        var huibi=$('.active_mess_right .huibi .num');
        var percent=parseFloat(huibi.attr('data-percent'))/100;
        $('.tags-list').click(function(){
            moneyRadio=$('.money-radio:checked');
            dateRadio=$('.date-radio:checked');
            payMoney=moneyRadio.val()?moneyRadio.val():0;
            price=(parseFloat(payMoney)*payNum).toFixed(2);
            tip=moneyRadio.attr('data-value');
            if(dateRadio.attr('class')){
                $('.date-end').html(dateRadio.attr('data-end'));
                $('.date-until').html(dateRadio.attr('data-until'));
            }
            if(Number(price)){
                $('.active_mess_right .price').html('￥'+price);
                huibi.html(parseInt(price)*percent);
            }
        });
        //数量选择
        var numBox=$('.number-box');
        var numYi=parseInt($('#sysl').text());
        numBox.children('input').val(payNum);
        $('label').click(function(){payNum=1;$('.number-box').children('input').val(payNum);});
        numBox.children('.jia').click(function(){
            moneyRadio=$('.money-radio:checked');
            dateRadio=$('.date-radio:checked');
            payMoney=moneyRadio.val();
            if(moneyRadio.attr('class')&&dateRadio.attr('class')){
                payNum++;
                if(payNum>=numYi){
                    payNum=numYi;
                }
                $('.number-box').children('input').val(payNum);
                price=(Number(payNum).mul(payMoney)).toFixed(2);
            }
        });
        numBox.children('.jian').click(function(){
            moneyRadio=$('.money-radio:checked');
            dateRadio=$('.date-radio:checked');
            payMoney=moneyRadio.val();
            if(moneyRadio.attr('class')&&dateRadio.attr('class')){
                payNum--;
                if(payNum<=0){
                    payNum=1;
                }
                $('.number-box').children('input').val(payNum);
                price=Number((payNum).mul(payMoney)).toFixed(2);
            }
            if(moneyRadio.attr('class')&&price){
                $('.active_mess_right .price').html('￥'+price);
                huibi.html(parseInt(price)*percent);
            }
        });
    })();
    //商品购买价格
    (function(){
        $('.goods_thumb_list .thumb_item img').hover(function(){
            $('.goods_thumb_list .thumb_item').removeClass('active');
            $(this).parents('.thumb_item').addClass('active');
            $(this).parents('.xq_img').children('.thumb').children('img').attr('src',$(this).attr('src'));
        });
        var numInput=$('.number-box .num');
        var sizeRadio=$('.size-radio:checked');
        var colorRadio=$('.color-radio:checked');
        var payNum=numInput.val();
        var paysize=colorRadio.val()?colorRadio.val():0;
        var price=Number(payNum).mul(colorRadio.val(paysize));
        var tip=sizeRadio.attr('data-value');
        var huibi=$('.goods_price');
        var percent=parseFloat(huibi.attr('data-percent'))/100;
        $('.tags-list').click(function(){
            sizeRadio=$('.size-radio:checked');
            colorRadio=$('.color-radio:checked');
            paysize=colorRadio.val()?colorRadio.val():0;
            price=(parseFloat(paysize)*payNum).toFixed(2);
            tip=sizeRadio.attr('data-value');
            if(colorRadio.attr('class')){
                $('.goods_price').html(price);
            }
        });
        //数量选择
        var numBox=$('.number-box');
        var numYi=parseInt($('#sysl').text());
        numBox.children('input').val(payNum);
        $('label').click(function(){payNum=1;$('.number-box').children('input').val(payNum);});
        numBox.children('.jia').click(function(){
            sizeRadio=$('.size-radio:checked');
            colorRadio=$('.color-radio:checked');
            paysize=colorRadio.val();
            if(sizeRadio.attr('class')&&colorRadio.attr('class')){
                payNum++;
                if(payNum>=numYi){
                    payNum=numYi;
                }
                $('.number-box').children('input').val(payNum);
                price=(Number(payNum).mul(paysize)).toFixed(2);
            }
            if(colorRadio.attr('class')&&price) {
                $('.goods_price').html(price);
                //huibi.html(parseInt(price)*percent);
            }
        });
        numBox.children('.jian').click(function(){
            sizeRadio=$('.size-radio:checked');
            colorRadio=$('.color-radio:checked');
            paysize=colorRadio.val();
            if(sizeRadio.attr('class')&&colorRadio.attr('class')){
                payNum--;
                if(payNum<=0){
                    payNum=1;
                }
                $('.number-box').children('input').val(payNum);
                price=Number((payNum).mul(paysize)).toFixed(2);
            }
            if(colorRadio.attr('class')&&price) {
                $('.goods_price').html(price);
                //huibi.html(parseInt(price)*percent);
            }
        });
    })();
    //评论
    (function(){
        /*
         * 点赞
         * */
        $('.zan-btn').one('click',function(){
            var num=parseInt($(this).children('.num').html());
            var that=this;
            $(this).children('.add').css('opacity',1).animate({top:'-14px',opacity:0},800,function(){
                $(that).children('.iconfont').css('color','red');
                $(that).children('.num').html(num+1);
            });
        });
        /*
         * 点击评论
         * */
        $('.pl_num').click(function(){
            var textareaBox=$(this).parents('.line').children('.textarea_box');
            textareaBox.show();
            textareaBox.children('textarea').focus();
        });
        $('.textarea_box textarea').keyup(function(){
            var numObj=$(this).siblings('.tip').children('.num');
            var numVal=parseInt(numObj.html());
            var textLen=$(this).val().length;
            if(textLen>=250){
                this.value=this.value.substr(0,250);
                textLen=250;
            }
            numObj.html(250-textLen);
        });
        /*
         * 评论框失去焦点
         * */
        $('.pl_list textarea').blur(function(){
            var that=this;
            setTimeout(function(){
                $(that).parents('.textarea_box').hide();
            },300);
        });
        $('.pl_btn').click(function(){
            if($(this).siblings('textarea').val()!=''){
                $('.pl_mask_tip').css('display','block').fadeOut(2000);
            }
        });
    })();
    //点击收藏
    (function(){
        var num=1;
        $('.collect').click(function(){
            if(num==1){
                $(this).children('.iconfont').css('color','#ffaa01');
                $(this).children('.font').html('取消收藏');
                num=2;
            }else if(num==2){
                $(this).children('.iconfont').css('color','#c8c8c8');
                $(this).children('.font').html('收藏');
                num=1;
            }
        });
    })();
    //课程详情选课
    $('.class_line').delegate('.select_btn','click',function(){
        var tabControl=$('.tabControl');
        var index=0;
        for(var i=0;i<tabControl.length;i++){
            if($(tabControl[i]).css('display')=='block'){
                index=i;
            }
        }
        var num=$(tabControl[index]).children('.item').children('input:checked').attr('data-num');
        $('.tabDetails').css('display','none').eq(num).css('display','block');
    });
    //搜索部分 筛选
    (function(){
        /*
         * 搜索部分
         * */
        //课程分类等标签选择
        $('.selector_tag .tag').click(function(){
            $(this).siblings('.tag').removeClass('select');
            $(this).addClass('select');
            if($(this).hasClass('has_area')){
                $(this).filter('.select').siblings('.area_box').css('display','block');
            }else{
                $(this).filter('.select').siblings('.area_box').css('display','none');
            }
        });
        //筛选前三个
        $('.shai .shai_item.radio').click(function(){
            $(this).siblings('.shai_item').removeClass('shai_curr').removeClass('.reversed');
            if($(this).hasClass('shai_curr')){
                $(this).toggleClass('reversed');
            }else{
                $(this).addClass('shai_curr');
            }
        });
        //筛选后面三个
        $('.select_s .s_children').click(function(){
            $('.shai .shai_item').removeClass('shai_curr').removeClass('.reversed');
            $(this).parents('.shai_item').addClass('shai_curr').children('.select_t').html($(this).html());
            $(this).siblings('.s_children').removeClass('s_curr');
            $(this).addClass('s_curr');
        });
        //价格确定按钮
        var priceStart= 0,priceEnd=0;
        var priceInput=$('.shai_item .price_box .price_input');
        priceInput.focus(function(){
            $(this).siblings('.price_confirm').css('display','block');
        });
        priceInput.blur(function(){
            if($(this).hasClass('price_start')){
                priceStart=$(this).val();
                if(isNaN(priceStart)){
                    $(this).val('');
                }else{
                    if(priceStart){
                        priceStart=Math.round(priceStart*100)/100;
                        $(this).val(priceStart);
                    }
                }
            }else if($(this).hasClass('price_end')){
                priceEnd=$(this).val();
                if(isNaN(priceEnd)){
                    $(this).val('');
                }else{
                    if(priceEnd){
                        priceEnd= Math.round(priceEnd*100)/100;
                        $(this).val(priceEnd);
                    }
                }
            }
        });
        $('.price_box .price_confirm').click(function(){
            $('.shai .shai_item').removeClass('shai_curr').removeClass('.reversed');
            $('#s_price').html(priceStart+'-'+priceEnd+'元').parents('.shai_item').addClass('shai_curr');
        });
    })();
    
    //有用+1
    $('.useful').click(function(){
        var num=parseInt($(this).children('.num').html());
        var that=this;
        $(this).children('.add').animate({fontSize:'14px',top:'-24px'},500,function(){
            $(this).css('display','none');
            $(that).css({color:'#ffaa01',border:'1px solid #ffaa01'}).children('.num').html(num+1);
        });
    });
    //登录
    $('.login_btn').click(function(){
        $('.mask-login').css('display','block');
        $('.loginBox').css('display','block');
        $('.zhuceBox').css('display','none');
    });

    $('.mask-login .mask,.close').click(function(){
        $('.mask-login,.mask-yuyue').css('display','none');
    });

    $('.newRegister').click(function(){
        $('.mask-login .loginBox').animate({opacity:0,zIndex:1},300,function(){
            $(this).css({opacity:1,display:'none'});
            $('.mask-login .zhuceBox').css('display','block').animate({opacity:1,zIndex:3},300);
        });
    });

    $('.oldLogin').click(function(){
        $('.mask-login .zhuceBox').animate({opacity:0,zIndex:1},500,function(){
            $(this).css({opacity:1,display:'none'});
        }).siblings('.mask-login .loginBox').css({display:'block',opacity:0}).animate({opacity:1,zIndex:3},500);
    });
    //预约报课
    $('.yuyue_btn').click(function(){
        $('.mask-yuyue').css('display','block');
    });
    $('.mask-yuyue .mask,.close').click(function(){
        $('.mask-yuyue').css('display','none');
    });
    //申请退课
    $('.request-btn').click(function(){
        $('.request-mask').css('display','block');
    });
    $('.request-mask .mask,.request-mask .close').click(function(){
        $('.request-mask').css('display','none');
    });
});